<?php


namespace App\Http\Controllers\Admin;


use App\DTOS\Admin\BanListDTO;
use App\Models\Game\BanIpList;
use App\Models\Game\BanList;
use App\Models\Game\UserMember;
use App\Models\Game\UserRole;
use App\Models\Role;
use App\Services\Game\BanListService;
use App\Utils\MyLog;
use App\Utils\ResponseUtil;

class BanIpListController extends \App\Http\Controllers\Controller
{
    private BanListService $banListService;

    /**
     * BanListController constructor.
     * @param BanListService $banListService
     */
    public function __construct(BanListService $banListService)
    {
        $this->banListService = $banListService;
    }

    public function add(){
        $input = request()->all();
        $ip = data_get($input,'ip');
        if (is_array($ip)) {
            $ip = implode(',', $ip);
        }
        $ips = array_unique(explode(',', $ip));
        $inserts = [];
        foreach($ips as $item){
            $insert = [];
            $insert['created_at'] = date("Y-m-d H:i:s");
            $insert['ip'] = trim($item);
            $insert['cause'] = data_get($input,'cause');
            $inserts[] = $insert;
        }
        BanIpList::query()->insert($inserts);
        return ResponseUtil::success();
    }


    public function delete($id){
        if (strpos($id, ',') !== false) {
            $idArray = explode(',', $id);
            BanIpList::query()->whereIn('id',$idArray)->delete();
        }else{
            BanIpList::query()->where('id',$id)->delete();
        }
        return ResponseUtil::success();
    }

    public function paginate()
    {
        $ip =data_get(request()->all(),'ip');
        $page=data_get(request()->all(),'page');
        $limit =data_get(request()->all(),'limit');
        $model = BanIpList::query();

        if ($ip){
            $model->where('ip',$ip);
        }

         $res= $model->orderByDesc('id')->paginate($limit,'*','page',$page);
        $data = $res->items();
        $res = [];
        $res['code'] = 0;
        $res['data'] = $data;
        $res['count'] = count($res);
        return ResponseUtil::json($res);
    }





    public function linspaginate(){
        $DTO = new BanListDTO(request()->all());
        $serverId =data_get(request()->all(),'gameArea');

        global $gameArea;$gameArea = $serverId;
        $paginate = $this->banListService->paginate($DTO);
        $data = $paginate->items();
        MyLog::info('区服ID'.json_encode($serverId));
        if($serverId>0){

        $customer_id = collect($data)->pluck('customer_id')->unique()->toArray();
        $members = UserMember::query()->whereIn('openid',$customer_id)->select(['id','openid'])->get()->toArray();
        $mainIds = collect($members)->pluck('id')->toArray();
        $roles = UserRole::query()->whereIn('memid',$mainIds)->select(['id','nick','memid'])->get()->toArray();
        MyLog::info('符合条件的角色数据'.json_encode($roles));
        foreach ($data as &$datum){
            $datum['status']  = data_get($datum,'enable_at') < time() ? '封禁中' : '已解封';
            $datum['customer_id']  = (string)$datum['customer_id'];

            $member = collect($members)->where('openid',$datum['customer_id'])->first();
            MyLog::info('账号ID'.json_encode($member));

            if($member){
            $role=  collect($roles)->where('memid',$member['id'])->toArray();
            $roleCollection = collect($role);
            $roleString = $roleCollection->map(function ($role) {
                return $role['id'] . ':' . $role['nick'];
            })->implode('|');
            $datum['roles'] = $roleString;
            }else{
                $datum['roles'] = null;

            }
        }
        }else{
            foreach ($data as &$datum){
                $datum['status']  = data_get($datum,'enable_at') < time() ? '封禁中' : '已解封';
                $datum['customer_id']  = (string)$datum['customer_id'];
            }

        }

        $res = [];
        $res['code'] = 0;
        $res['data'] = $data;
        $res['count'] = $paginate->total();
        return ResponseUtil::json($res);
    }
}
